const { join } = require('path')
const resolve = require('rollup-plugin-node-resolve')
const babel = require('rollup-plugin-babel')
const cwd = __dirname

const baseConfig = {
  // external: ['nervjs'],
  output: [
    {
      file: join(cwd, 'dist/jim.js'),
      format: 'umd',
      name: 'Jim',
      sourcemap: true,
      exports: 'named'
    }
  ],
  dest:'',
  entry:require('./app/jim/index.js'),
  plugins: [
    resolve({
      preferBuiltins: false,
      alias:{
        'jim':join(cwd,'app/jim'),
        'src':join(cwd,'app/src'),
        'utils':join(cwd,'app/utils'),
      },
    }),
    babel({
      babelrc:true,
      runtimeHelpers: true ,
      exclude: 'node_modules/**'
    })
  ],
}
const esmConfig = Object.assign({}, baseConfig, {
  output: Object.assign({}, baseConfig.output, {
    sourcemap: true,
    format: 'es',
    file: join(cwd, 'dist/jim.esm.js')
  })
})

function rollup () {
  const target = process.env.TARGET

  if (target === 'umd') {
    return baseConfig
  } else if (target === 'esm') {
    return esmConfig
  } else {
    return [baseConfig, esmConfig]
  }
}
module.exports = rollup()
